﻿using AdinZ.Web.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/4/6 21:02:17
* */
namespace Web
{
    public partial class download : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {



            //download.aspx?id=1134
            //download.aspx?id=1134&chapter=120443
            int bookID=0;
            int chapterID=0;
            string book = Request["id"];
            string chapter = Request["chapter"];

            string action = Request["action"]; if (string.IsNullOrEmpty(action)) { action = "nont"; }
            if (action==("reader")) 
            {
             string sql = "SELECT TOP 1 * FROM  [reader_update] WHERE [type] ='android' ORDER BY  ID desc";


             DataSet ds = AdinZ.DB.DbHelpers.GetDataSet(sql);
                 try
                 {
                     if (ds.Tables[0].Rows.Count > 0)
                     {
                         float version = float.Parse(ds.Tables[0].Rows[0]["version"].ToString());
                         string title = ds.Tables[0].Rows[0]["title"].ToString();
                         string body = ds.Tables[0].Rows[0]["body"].ToString();
                         string url = ds.Tables[0].Rows[0]["url"].ToString();
                         string update = ds.Tables[0].Rows[0]["update"].ToString();
                         Response.Clear();
                         Response.ContentType = "application/octet-stream";
                         Response.Status = "301 Moved Permanently";
                         Response.AddHeader("Location", url);
                         Response.End();
                     }
                     else
                     {
                         Response.Write("<script type=\"text/javascript\">alert('未找到下载文件');top.location.href='/'</script>");
                     }
                 }
                 catch {
                     Response.Write("<script type=\"text/javascript\">alert('下载遇到问题失败');top.location.href='/'</script>");
                 }



            }

            if (!string.IsNullOrEmpty(book))
                bookID = int.Parse(book);
            if (!string.IsNullOrEmpty(chapter))
                chapterID = int.Parse(chapter);
            if (chapterID > 0)
            {
                AdinZ.Common.NetServer.Get(siteConfig.SiteHost + "/plug/number.ashx?action=downloads&id=" + bookID);
                Novel.Sys.BLL.novel_chapter bll = new Novel.Sys.BLL.novel_chapter();
                Novel.Sys.Model.novel_chapter model = new Novel.Sys.Model.novel_chapter();
                model = bll.GetModel(chapterID);
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.Status = "301 Moved Permanently";
                Response.AddHeader("Location", siteConfig.FileHost + model.Articlepath);
                Response.End();
                //按章下载
            }
            else if (bookID > 0)
            {
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("all.txt"));
                string[] str = new string[] { "http://file.xiaoshuofang.com/chapter/461.txt", 
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", 
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt",
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt",
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", 
                    "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", 
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", 
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt",
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt",
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", 
                    "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", 
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt",
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt",
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", 
                    "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", 
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt",
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt",
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", 
                    "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", 
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt",
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt",
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", 
                    "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt",
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt",
                    "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", 
                    "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt", "http://file.xiaoshuofang.com/chapter/462.txt", "http://file.xiaoshuofang.com/chapter/463.txt", "http://file.xiaoshuofang.com/chapter/464.txt" };
                for (int i = 0; i < str.Length; i++)
                {
                    string data = AdinZ.Common.NetServer.Get(str[i]);
                    MemoryStream strm = new MemoryStream(System.Text.Encoding.Default.GetBytes(data));
                    Byte[] bs = new Byte[strm.Length];
                    strm.Read(bs, 0, (int)strm.Length);
                    Response.BinaryWrite(bs);
                }



               Response.End();

                //下载全本
              //  Response.Write("<script type=\"text/javascript\">alert('暂不支持下载全本');top.location.href='/'</script>");
            }
        }
    }
}